package com.jy.study;

import cn.hutool.core.map.MapUtil;

import java.util.*;

/**
 * @Classname MapUtils
 * @Description Map工具类的例子
 * @Date 2019/10/10 17:34
 * @Created by  jiaoyong
 */
public class MapUtils {

    public static void main(String[] args) {

        //isEmpty、isNotEmpty 判断Map为空和非空方法，空的定义为null或没有值（常用也常见，这个就不举例了）

        //快速创建多种类型的HashMap实例
        //@param boolean Map的Key是否有序，有序返回 {@link LinkedHashMap}，否则返回 {@link HashMap}
        Map<String, Integer> map1 = MapUtil.newHashMap();
        map1.put("a",1);
        map1.put("b",1);
        map1.put("c",1);

        Map<String, Integer> map2 = MapUtil.newHashMap(false);
        map2.put("a",2);
        map2.put("b",2);

        //创建自定义的Map类型的Map
        Map<String, Integer> map3 = MapUtil.newHashMap(3);
        map3.put("a",3);

        Map<String, Integer> map4 = MapUtil.createMap(map3.getClass());
        map4.put("Abc",100);
        map4.put("Bbc",200);
        map4.put("Cbc",300);

        //将一个或多个键值对加入到一个新建的Map中
        Map<Object, Object> colorMap = MapUtil.of(new String[][] {
                {"RED", "#FF0000"},
                {"GREEN", "#00FF00"},
                {"BLUE", "#0000FF"}
        });

        //toListMap 行转列，合并相同的键，值合并为列表，将Map列表中相同key的值组成列表做为Map的value
        List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
        //map1 = {a=1, b=1, c=1}
        list.add(map1);
        //map2 = {a=2, b=2}
        list.add(map2);
        //map3 = {a=3}
        list.add(map3);
        //map5 = {a=[1, 2, 3], b=[1, 2], c=[1]}
        Map<String, List<Integer>> map5 = MapUtil.toListMap(list);

        //toMapList 列转行。将Map中值列表分别按照其位置与key组成新的map
        Map<String, List<Integer>> map6 = MapUtil.createMap(map5.getClass());
        map6.put("a", Arrays.asList(1,2,3));
        map6.put("b", Arrays.asList(1,2));
        map6.put("c", Arrays.asList(1));
        //map6 = {a=[1, 2, 3], b=[1, 2], c=[1]}
        //map7 = [{a=1, b=1, c=1}, {a=2, b=2}, {a=3}]
        List<Map<String, Integer>> map7 = MapUtil.toMapList(map6);

        //将Map按照给定的分隔符转换为字符串,entry之间的连接符和kv之间的连接符都可以自定义
        //map6 = {a=[1, 2, 3], b=[1, 2], c=[1]}
        //joinMap6 = a:[1, 2, 3];b:[1, 2];c:[1]
        String joinMap6 = MapUtil.join(map6, ";", ":");

        //Map的键和值互换(键值类型必须一致)
        Map<Integer, Integer> map8 = MapUtil.createMap(map6.getClass());
        map8.put(520,1314);
        map8.put(456,123);
        //map8 = {520=1314, 456=123}
        //reverse = {1314=520, 123=456}
        Map<Integer, Integer> reverse = MapUtil.reverse(map8);

    }
}
